define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'businessmanage/business/index' + location.search,
                    add_url: 'businessmanage/business/add',
                    edit_url: 'businessmanage/business/edit',
                    del_url: 'businessmanage/business/del',
                    multi_url: 'businessmanage/business/multi',
                    import_url: 'businessmanage/business/import',
                    table: 'business',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'b_name', title: __('B_name'), operate: 'LIKE'},
                        {field: 'c_name', title: __('C_name'), operate: 'LIKE'},
                        {field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'bid', title: __('Btype_id'),operate: false},
                        {field: 'btype_id', title: __('Btype_id'),addclass:"selectpage", extend:'data-source="businessmanage/business/index?action=getType"',visible: false},
                        {field: 'point', title: __('Point'),cellStyle: function (value, row, index) { return {css: {"color": "red"}};}},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'expirationtime', title: __('Expirationtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __("Status"), searchList: {"0":'未领取',"1":'已领取'}, formatter: Table.api.formatter.status},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pushswitch', title: __('Pushswitch'), searchList: {"0":'未推送所有用户',"1":'推送所有用户'}, table: table, formatter: Table.api.formatter.toggle},

                        {field: 'operate', 
                            title: __('Operate'), table: table, 
                            events: Table.api.events.operate, 
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name:'obtain',//名称 
                                    text:'撤销领取',
                                    title:'撤销领取',
                                    confirm: '确认撤销吗?',
                                    classname: 'btn btn-xs btn-danger btn-view btn-ajax',
                                    icon: 'fa fa-recycle',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'businessmanage/business/revoke',//接口地址 控制器名/方法名
                                    refresh:true,
                                    hidden:function (row){if(row.status == 0)return true;},
                                },{
                                name: 'select',
                                text: '推送用户',
                                icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                extend: 'data-area=\'["80%","80%"]\' data-shade=\'[0.5,"#000"]\' data-toggle="tooltip"', // 自定义弹窗尺寸、阴影等
                                url: 'businessmanage/select/index',
                                hidden:function (row){if(row.pushswitch == 1)return true;},
                            }],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'businessmanage/business/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'businessmanage/business/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'businessmanage/business/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
